package ch07;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class VoteMain {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String name = "";
        String topName = "";
        int count = -1;
        Map<String,Integer> map = new HashMap<>();// String 人名  Integer 票数
        do{
            System.out.print("请输入投票的姓名,输入 n 结束: ");
            name = input.next();
            if (name.equals("n")) break;
            if (!map.containsKey(name)){
                map.put(name,1);
            }else{
                map.put(name,1+map.get(name));
            }
//            int num = !map.containsKey(name)?1:1+map.get(name);
//            map.put(name,num);
//            int num = !map.containsKey(name)?0:map.get(name);
//            map.put(name,num+1);
        }while(true);
        System.out.println("投票结果如下: ");
        for (Map.Entry<String,Integer> entry:map.entrySet()) {
            System.out.println(entry.getKey()+" : "+entry.getValue());
            if (entry.getValue()>count){
                count = entry.getValue();
                topName = entry.getKey();
            }
        }
        System.out.println(topName+"当选，票数是："+count);
    }
}
